$(function () {
    var dept_datagrid = $("#dept_datagrid");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");


    dept_datagrid.datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#dept_toolbar',
        url: '/department/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '部门编号', field: 'sn', width: '100'},
            {title: '部门名称', field: 'name', width: '100'},
            {
                title: '状态', field: 'state', width: '100', formatter: function (value, row, index) {
                    return value ? "<font color='green'>在职</font>" : "<font color='red'>离职</font>"
                }
            }
        ]],
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: "废除"
                })
            } else {
                $("#btn_state").linkbutton({
                    text: "恢复"
                })
            }
        }
    })


    dept_dialog.dialog({
        width: 300,
        height: 200,
        buttons: '#dept_buttons',
        closed: true,
        onClose: function () {
            dept_form.form("clear")
        }
    })

    var methodObj = {
        //添加按钮
        add: function () {
            dept_dialog.dialog("open");
            dept_dialog.dialog('setTitle', '新增部门');
        },
        //编辑按钮
        edit: function () {
            var data = dept_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }

            dept_form.form('load', data);

            dept_dialog.dialog("open");
            dept_dialog.dialog('setTitle', '编辑部门');
        },
        //离职按钮
        status: function () {
            var row = dept_datagrid.datagrid("getSelected");
            var text = $("#btn_state").text();
            if (!row) {
                $.messager.alert("温馨提示", "请选择要设置的数据", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要设置" + text + "吗？", function (r) {
                if (r) {
                    $.get("/department/status.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "设置成功", "info", function () {
                                dept_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //刷新按钮
        reload: function () {
            dept_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            dept_dialog.dialog("close");
        },
        //添加保存按钮
        save: function () {
            dept_form.form('submit', {
                url: '/department/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            dept_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        }
    }


    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

})

